package ink.tiptop.configuration;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;

import java.util.Locale;
/**
 * @author ink.tiptop.zjf
 * @Title: Ticket
 * @Package ink.tiptop.common.interceptor
 * @Description:  mvc配置,用于注册拦截器啥的
 * @date 2020-06-25 22:01
 */

@Configuration
public class WebConfiguration extends WebMvcConfigurationSupport {
    @Bean
    //方法的返回值是bean容器的实例
    public LocaleResolver localeResolver(){      //LocaleResolver是一个父接口，方法的返回值只能定成这个名字，以前写springMVC.xml时id必须写localeResolver是一个道理,如果改成别的名字，DispacherServlet就没办法加载它
        CookieLocaleResolver localeResolver=new CookieLocaleResolver();     //CookieLocalResolver与SessionLocaleResolver类似
        localeResolver.setDefaultLocale(Locale.CHINA);  //设置默认的语言种类
        localeResolver.setCookieMaxAge(3600);           //失效
        return localeResolver;
    }

    @Bean
    public LocaleChangeInterceptor interceptor(){ //方法名不重要，但方法的返回值是不能随意取
        return new LocaleChangeInterceptor();
    }
    @Bean
    public LoginInterceptor loginInterceptor(){ //方法名不重要，但方法的返回值是不能随意取
        return new LoginInterceptor();
    }
    @Bean
    public LogInterceptor logInterceptor(){ //方法名不重要，但方法的返回值是不能随意取
        return new LogInterceptor();
    }

    // /swagger*/**
    // /v2/**
    // /webjars/**
    //继承了WebMvcConfigurationSupport，所以要重写两个方法
    //除了有拦截器实例，还要把实例加载到拦截器栈里面去
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        String[] exclude = {"/swagger*/**", "/v2/**", "/webjars/**","/swagger-ui.html"};
        registry.addInterceptor(logInterceptor());
        //添加配置的语言变化拦截器
        registry.addInterceptor(interceptor());
        // 注册一个自定义拦截器
        registry.addInterceptor(loginInterceptor()).excludePathPatterns(exclude);
        // 响应数据拦截器
//        registry.addInterceptor(respInterceptor());


    }

    //  /swagger*/**
    //  /v2/**
    //  /webjars/**
    //如下，spring拦截器，需配置如下：
    //因为配置了拦截器，所以springMVC的dispatcherServlet就会启动，原来静态资源加载就会出问题了
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        //处理静态资源访问
        registry.addResourceHandler("/**")
                // 默认的就是这个
                .addResourceLocations("classpath:/static/");
        //根目录下的任何静态资源被请求时，都到/static/目录下去找静态资源

        // 放行swagger2
//        registry.addResourceHandler("/statics/**").addResourceLocations("classpath:/statics/");
        // 解决 SWAGGER 404报错
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");

        super.addResourceHandlers(registry);    //registry要加载父类的registry里面
    }
}
